package com.bwie.activity.service.impl;

import com.bwie.activity.mapper.ActivityMapper;
import com.bwie.activity.pojo.bo.ActivityRuleBo;
import com.bwie.activity.pojo.bo.JoinUserSkuBo;
import com.bwie.activity.pojo.bo.LogBo;
import com.bwie.activity.pojo.bo.LotteryLogBo;
import com.bwie.activity.service.ActivityService;
import com.bwie.common.constants.Constants;
import com.bwie.common.exceptions.BusinessException;
import com.bwie.common.pojo.req.*;
import com.bwie.common.pojo.res.ActivityRes;
import com.bwie.common.pojo.res.PrizeRes;
import com.bwie.common.pojo.res.RuleRes;
import com.bwie.common.result.PageResult;
import com.bwie.common.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ActivityServiceImpl implements ActivityService {
    private final ActivityMapper activityMapper;
    private final HttpServletRequest request;
    private final RedisTemplate<String, Object> redisTemplate;
    /**
     * 获取活动列表
     * @param activityReq
     * @return
     */
    @Override
    public PageResult<ActivityRes> activityList(ActivityReq activityReq) {
        PageHelper.startPage(activityReq.getPageNum(),activityReq.getPageSize());
        List<ActivityRes> list = activityMapper.activityList(activityReq);
//        // 计算连续序号
//        int startNumber = (activityReq.getPageNum() - 1) * activityReq.getPageSize() + 1;
//        for (int i = 0; i < list.size(); i++) {
//            list.get(i).setActivityId(startNumber + i);
//        }
        PageInfo<ActivityRes> pageInfo = new PageInfo<>(list);
        return PageResult.toPageResult(pageInfo.getTotal(), list);
    }

    /**
     * 获取奖品规则列表
     * @return
     */
    @Override
    public List<RuleRes> getPrizeRules(EditNpReq editNpReq) {
        List<RuleRes> prizeRules = activityMapper.getPrizeRules();
        if (editNpReq.getPrizeNumber() != null && editNpReq.getPrizeProbability() != null){
            RuleRes ruleRes = prizeRules.get(editNpReq.getIndex());
            ruleRes.setPrizeNumber(editNpReq.getPrizeNumber());
            ruleRes.setPrizeProbability(editNpReq.getPrizeProbability());
            prizeRules.set(editNpReq.getIndex(), ruleRes);
        }
        return prizeRules;
    }

    /**
     * 修改规则
     * @param updateRuleReq
     */
    @Override
    public void updateRule(UpdateRuleReq updateRuleReq) {
        activityMapper.updateRule(updateRuleReq);
    }

    /**
     * 获取抽奖类型下拉框
     * @return
     */
    @Override
    public List<ActivityRes> getActivity() {
        return activityMapper.getActivity();
    }

    /**
     * 获取抽奖类型下拉框
     * @return
     */
    @Override
    public List<RuleRes> getRule() {
        return activityMapper.getRule();
    }

    /**
     * 添加活动
     * @param activityAddReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addActivity(ActivityAddReq activityAddReq) {
        // 添加活动
        activityMapper.addActivity(activityAddReq);

        // 批量添加活动规则中间表
        ActivityRuleBo activityRuleBo = new ActivityRuleBo();
        for (int i = 0; i < activityAddReq.getRuleIdList().size(); i++) {
            activityRuleBo.setActivityId(activityAddReq.getActivityId());
            activityRuleBo.setRuleId(activityAddReq.getRuleIdList().get(i));
            activityRuleBo.setPrizeNumber(activityAddReq.getPrizeNumberList().get(i));
            activityRuleBo.setPrizeProbability(activityAddReq.getPrizeProbabilityList().get(i));
            activityMapper.addActivityRule(activityRuleBo);
        }

        // 参与用户为部分时，添加sku
        if (activityAddReq.getJoinUser() == Constants.JOIN_USER_NUM){
            // 判空
            if (activityAddReq.getUserGarde() == null
                    || activityAddReq.getUserIsVip() == null
                    || activityAddReq.getExpendScore() == null){
                throw new BusinessException(Constants.JOIN_USER_VELUE_NULL);
            }
            JoinUserSkuBo joinUserSkuBo = new JoinUserSkuBo();
            BeanUtils.copyProperties(activityAddReq,joinUserSkuBo);
            activityMapper.addJoinUserSku(joinUserSkuBo);
        }

        // 添加记录表
        LogBo logBo = new LogBo();
        logBo.setActivityId(activityAddReq.getActivityId());
        logBo.setUserId(TokenUtils.getUser(request, redisTemplate).getUserId());
        activityMapper.addLog(logBo);
    }

    /**
     * 获取奖品信息
     * @param prizeReq
     * @return
     */
    @Override
    public List<PrizeRes> getRuleByUserIdId(PrizeReq prizeReq) {
        return activityMapper.getRuleByUserIdId(prizeReq);
    }

    /**
     * 添加抽奖记录
     * @param lotteryLogReq
     */
    @Override
    public void addLotteryLog(LotteryLogReq lotteryLogReq) {
        LotteryLogBo lotteryLogBo = new LotteryLogBo();
        lotteryLogBo.setUserId(TokenUtils.getUser(request,redisTemplate).getUserId());
        BeanUtils.copyProperties(lotteryLogReq, lotteryLogBo);
        activityMapper.addLotteryLog(lotteryLogBo);
    }
}
